<?php
// function __autoload($class_name) {
//         include 'class.' . strtolower($class_name) . '.php';
// }
require_once('class.database.php');

class UsulanPegDao {

	public function getAll($nip){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan_pegawai  WHERE nip='$nip'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
	}

    public function getAllToProses($nip){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan_pegawai  WHERE nip='$nip' AND isProses=0";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }

    public function getAllSudahProses($nip){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan_pegawai  WHERE nip='$nip' AND isProses=1";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }

    
	public function getById($id){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_usulan_pegawai WHERE id='$id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return $result->fetch();
        }

        return false;
	}

    public function createUsulan(UsulanPeg $usulan){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $date_created=date('Y-m-d H:i:s');

        $query="INSERT INTO t_usulan_pegawai(nip,nipAtasan,tglAwal,tglAkhir,id_cuti,id_tujuan,date_created) ";
        $query.=" VALUES('$usulan->nip','$usulan->nipAtasan','$usulan->tglAwal','$usulan->tglAkhir','$usulan->id_cuti','$usulan->id_tujuan','$date_created')";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

	public function updateUsulan(UsulanPeg $usulan){
		$db=Database::getInstance();
        $con=$db->getConnection(1);

        $date_updated=date('Y-m-d H:i:s');

        $query="UPDATE t_usulan_pegawai SET nipAtasan='$usulan->nipAtasan',tglAwal='$usulan->tglAwal',tglAkhir='$usulan->tglAkhir',id_cuti='$usulan->id_cuti',id_tujuan='$usulan->id_tujuan',date_updated='$date_updated' WHERE id='$usulan->id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
	}

    public function deleteUsulan($id){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="DELETE FROM t_usulan_pegawai WHERE id='$id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function updateUsulanProses($id){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="UPDATE t_usulan_pegawai SET isProses=1 WHERE id='$id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

}

class UsulanPeg {
	var $id;
	var $nip;
    var $nipAtasan;
	var $tglAwal;
	var $tglAkhir;
	var $id_cuti;
	var $id_tujuan;

}

?>